﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Management.Automation;
using PowerOBIProvider;
using PowerOBIProvider.OBI;
using PowerOBIPRovider.OBIWebReference;

namespace PowerOBIProvider.Cmdlts 
{
    [Cmdlet(VerbsCommon.Get,"Account")]
    public class GetAccountCmdlt: BaseOBICmdlt
    {

        private string drive = null;
        [Parameter(Mandatory = false, Position = 1)]
        [ValidateNotNullOrEmpty]
        public string Drive
        {
            get
            {
                return drive;
            }
            set
            {
                drive = value;
            }
        }
        private bool includeGroups = true;
        [Parameter(Mandatory = false, Position = 2)]
        [ValidateNotNullOrEmpty]
        public SwitchParameter IncludeGroups
        {
            get
            {
                return includeGroups;
            }
            set
            {
                includeGroups = value;
            }

        }
        private bool includeUsers = false;
        [Parameter(Mandatory = false, Position = 3)]
        [ValidateNotNullOrEmpty]
        public SwitchParameter IncludeUsers
        {
            get
            {
                return includeUsers;
            }
            set
            {
                includeUsers = value;
            }
        }
        
        
        
        
        protected override void ProcessRecord()
        {
            PSOBIDriveInfo obiDrive = null;
            if (drive == null)
            {
                obiDrive = GetOBIDrive(SessionState.Path.CurrentLocation);
            }
            else
            {
                obiDrive = SessionState.Drive.Get(drive) as PSOBIDriveInfo;
            }
            Account[] accounts = null;
            try
            {
                if (obiDrive != null)
                {
                    accounts = obiDrive.ObiWSClient.GetAccountsDatabase(includeGroups, includeUsers);
                    foreach (Account account in accounts)
                        WriteObject(account);

                }
                else
                {
                    WriteError(new ErrorRecord(new Exception("Please specify the OBI drive either by -drive param or navigating to it via Set-Location cmdlt."), "0", ErrorCategory.InvalidArgument, null));
                }

            }
            catch (Exception ex)
            {
                WriteError(new ErrorRecord(ex,"0",ErrorCategory.InvalidOperation,null) );
            }
            


        }
    }
}
